//
//  XMPicwordCell.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/7/2.
//

#import "XMPicwordCell.h"

@interface XMPicwordCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *coverImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *avatarImgView;//用户头像
@property (nonatomic, strong) UILabel *nameLabel;//用户标题
@property (nonatomic, strong) UILabel *likeNumLabel;//点赞数量
@property (nonatomic, strong) UIButton *likeBtn;//用户的点赞

@property (nonatomic, strong) XMPicwordModel *picwordModel;

@end

@implementation XMPicwordCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    // 初始化图片视图
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.coverImgView];
    // 初始化文本标签
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.avatarImgView];
    [self.bgView addSubview:self.nameLabel];
    [self.bgView addSubview:self.likeBtn];
    [self.bgView addSubview:self.likeNumLabel];
}

- (void)setContentWithPicwordModel:(XMPicwordModel *)picwordModel {
    self.picwordModel = picwordModel;
    _likeNumLabel.text = picwordModel.likeCount;
    CGFloat height = [picwordModel.title getHeightWithFont:KScaleWidth(16.0) withWidth:_titleLabel.width];
    self.titleLabel.height = height + KScaleWidth(5.0);
    self.titleLabel.text = picwordModel.title;
    [self setNeedsLayout];
    [_coverImgView sd_setImageWithURL:[NSURL URLWithString:picwordModel.coverImageUrl]];
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:picwordModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    self.nameLabel.text = picwordModel.nickname;
    _likeBtn.selected = [picwordModel.liked isEqualToString:@"1"];
    _avatarImgView.y = _titleLabel.maxY + KScaleWidth(10.0);
    _nameLabel.y = _avatarImgView.y;
    _likeBtn.y = _nameLabel.y;
    _likeNumLabel.y = _likeBtn.y;
    _bgView.height = _titleLabel.maxY + KScaleWidth(44.0);
}

- (void)likeAction:(UIButton *)button {
    self.likeBlock ? self.likeBlock() : nil;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = KScaleWidth(20.0);
    }
    return _bgView;
}

- (UIImageView *)coverImgView {
    if (!_coverImgView) {
        _coverImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(210.0))];
        _coverImgView.contentMode = UIViewContentModeScaleAspectFill;
        _coverImgView.layer.masksToBounds = YES;
        _coverImgView.layer.cornerRadius = KScaleWidth(13.0);
    }
    return _coverImgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), CGRectGetMaxY(self.coverImgView.frame) + KScaleWidth(10.0), self.width - KScaleWidth(20.0), KScaleWidth(20.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), self.titleLabel.maxY + KScaleWidth(8.0), KScaleWidth(20.0), KScaleWidth(20.0))];
        _avatarImgView.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImgView.layer.masksToBounds = YES;
        _avatarImgView.layer.cornerRadius = 15;
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(3.0), self.avatarImgView.y, self.width/3*2 - _avatarImgView.maxX - KScaleWidth(3.0), _avatarImgView.height) font:KFontSize(14.0) textColor:kColorHEX(0x999999)];
    }
    return _nameLabel;
}

- (UIButton *)likeBtn {
    if (!_likeBtn) {
        _likeBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(self.width - KScaleWidth(22.0) - KScaleWidth(32), self.nameLabel.y, KScaleWidth(32.0), _nameLabel.height) image:@"evaluate_like_icon" selectImg:@"evaluate_like_icon_select"];
        [_likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likeBtn;
}

- (UILabel *)likeNumLabel {
    if (!_likeNumLabel) {
        _likeNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width - KScaleWidth(28.0), self.likeBtn.y, KScaleWidth(28.0), _likeBtn.height) font:KFontSize(12.0) textColor:kColorHEX(0x999999)];
    }
    return _likeNumLabel;
}

@end
